
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>urllib.error --- urllib.request 引发的异常类 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="urllib.robotparser --- robots.txt 语法分析程序" href="urllib.robotparser.html" />
    <link rel="prev" title="urllib.parse --- Parse URLs into components" href="urllib.parse.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/urllib.error.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="urllib.robotparser.html" title="urllib.robotparser --- robots.txt 语法分析程序"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="urllib.parse.html" title="urllib.parse --- Parse URLs into components"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">互联网协议和支持</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-urllib.error">
<span id="urllib-error-exception-classes-raised-by-urllib-request"></span><h1><a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> --- urllib.request 引发的异常类<a class="headerlink" href="#module-urllib.error" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/urllib/error.py">Lib/urllib/error.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> 模块为 <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a> 所引发的异常定义了异常类。 基础异常类是 <a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>。</p>
<p>下列异常会被 <a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> 按需引发：</p>
<dl class="exception">
<dt id="urllib.error.URLError">
<em class="property">exception </em><code class="sig-prename descclassname">urllib.error.</code><code class="sig-name descname">URLError</code><a class="headerlink" href="#urllib.error.URLError" title="永久链接至目标">¶</a></dt>
<dd><p>处理程序在遇到问题时会引发此异常（或其派生的异常）。 它是 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 的一个子类。</p>
<dl class="attribute">
<dt id="urllib.error.URLError.reason">
<code class="sig-name descname">reason</code><a class="headerlink" href="#urllib.error.URLError.reason" title="永久链接至目标">¶</a></dt>
<dd><p>此错误的原因。 它可以是一个消息字符串或另一个异常实例。</p>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span><a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> 已被设为 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> 的子类。</p>
</div>
</dd></dl>

<dl class="exception">
<dt id="urllib.error.HTTPError">
<em class="property">exception </em><code class="sig-prename descclassname">urllib.error.</code><code class="sig-name descname">HTTPError</code><a class="headerlink" href="#urllib.error.HTTPError" title="永久链接至目标">¶</a></dt>
<dd><p>虽然是一个异常 (<a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> 的一个子类)，<a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> 也可以作为一个非异常的文件类返回值 (与 <a class="reference internal" href="urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> 返所回的对象相同)。 这适用于处理特殊 HTTP 错误例如作为认证请求的时候。</p>
<dl class="attribute">
<dt id="urllib.error.HTTPError.code">
<code class="sig-name descname">code</code><a class="headerlink" href="#urllib.error.HTTPError.code" title="永久链接至目标">¶</a></dt>
<dd><p>一个 HTTP 状态码，具体定义见 <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a>。 这个数字值对应于存放在 <a class="reference internal" href="http.server.html#http.server.BaseHTTPRequestHandler.responses" title="http.server.BaseHTTPRequestHandler.responses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">http.server.BaseHTTPRequestHandler.responses</span></code></a> 代码字典中的某个值。</p>
</dd></dl>

<dl class="attribute">
<dt id="urllib.error.HTTPError.reason">
<code class="sig-name descname">reason</code><a class="headerlink" href="#urllib.error.HTTPError.reason" title="永久链接至目标">¶</a></dt>
<dd><p>这通常是一个解释本次错误原因的字符串。</p>
</dd></dl>

<dl class="attribute">
<dt id="urllib.error.HTTPError.headers">
<code class="sig-name descname">headers</code><a class="headerlink" href="#urllib.error.HTTPError.headers" title="永久链接至目标">¶</a></dt>
<dd><p>导致了 <a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> 的特定 HTTP 请求的 HTTP 响应头。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.4 新版功能.</span></p>
</div>
</dd></dl>

</dd></dl>

<dl class="exception">
<dt id="urllib.error.ContentTooShortError">
<em class="property">exception </em><code class="sig-prename descclassname">urllib.error.</code><code class="sig-name descname">ContentTooShortError</code><span class="sig-paren">(</span><em class="sig-param">msg</em>, <em class="sig-param">content</em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.error.ContentTooShortError" title="永久链接至目标">¶</a></dt>
<dd><p>此异常会在 <a class="reference internal" href="urllib.request.html#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a> 函数检测到已下载的数据量小于期待的数据量（由 <em>Content-Length</em> 头给定）时被引发。 <code class="xref py py-attr docutils literal notranslate"><span class="pre">content</span></code> 属性中将存放已下载（可能被截断）的数据。</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="urllib.parse.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code> --- Parse URLs into components</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="urllib.robotparser.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.robotparser</span></code> --- robots.txt 语法分析程序</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/urllib.error.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="urllib.robotparser.html" title="urllib.robotparser --- robots.txt 语法分析程序"
             >下一页</a> |</li>
        <li class="right" >
          <a href="urllib.parse.html" title="urllib.parse --- Parse URLs into components"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" >互联网协议和支持</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>